import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.util.*;
import java.util.List;

public class FrontDesk extends JDialog {
    public FrontDesk(JFrame frame){
        super(frame,"前台操作界面",true);
        setBounds(720,405,350,400);
        setLayout(new FlowLayout());

        Font font = new Font("SimSun",Font.BOLD, 15);
        //---------------------------------------------------------------------------------------
        JButton filmList = new JButton("列出正在上映影片");
        filmList.setPreferredSize(new Dimension(240, 60));
        filmList.setFont(font);
        //---------------------------------------------------------------------------------------
        JButton sessionsList = new JButton("列出场次信息");
        sessionsList.setPreferredSize(new Dimension(180, 60));
        sessionsList.setFont(font);

        JButton filmSessionsList = new JButton("列出指定电影场次信息");
        filmSessionsList.setPreferredSize(new Dimension(300, 60));
        filmSessionsList.setFont(font);

        JButton ticket = new JButton("售票");
        ticket.setPreferredSize(new Dimension(120, 60));
        ticket.setFont(font);

        JButton returnTicket = new JButton("退票");
        returnTicket.setPreferredSize(new Dimension(120, 60));
        returnTicket.setFont(font);

        JButton exitLogIN = new JButton("退出登录");
        exitLogIN.setPreferredSize(new Dimension(120, 60));
        exitLogIN.setFont(font);
        //---------------------------------------------------------------------------------------
        JComboBox<String> box = new JComboBox<>();
        JLabel label = new JLabel("选择近一天还是三天还是一周的场次信息");
        box.addItem("近一周");
        box.addItem("近三天");
        box.addItem("近一天");

        add(filmList);
        add(sessionsList);
        add(label);
        add(box);
        add(filmSessionsList);
        add(ticket);
        add(returnTicket);
        add(exitLogIN);

        filmList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog filmList = new JDialog(frame,"排片表",true);
                filmList.setBounds(720,405,1080,400);
                Container c = filmList.getContentPane();
                c.setLayout(new FlowLayout());
                for(Map.Entry<String, java.util.List<String>> entry : Film.getFilmList().entrySet()){
                    String key = entry.getKey();
                    if(Film.getFilmList().get(key).get(5).equals("已上映")) {
                        List<String> values = Film.getFilmList().get(key);
                        JLabel label = new JLabel(key+"||"+values.get(0)+"||"+values.get(1)+"||"+values.get(2)+"||"+values.get(3)+"||"+values.get(4)+"||");
                        c.add(label);
                    }
                }
                filmList.setVisible(true);
            }
        });

        sessionsList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                LocalDateTime nowDateTime = LocalDateTime.now();
                JDialog ArrangementList = new JDialog(frame,"排片表",true);
                ArrangementList.setBounds(720,405,400,1080);
                Container c = ArrangementList.getContentPane();
                c.setLayout(new FlowLayout());

                LocalDate nowDate = nowDateTime.toLocalDate();




                if(box.getSelectedItem().equals("近一周")) {
                    for (Map.Entry<List<String>, List<String>> entry : Cinema.getCinemaMap().entrySet()) {
                        List<String> key = entry.getKey();
                        List<String> values = Cinema.getCinemaMap().get(key);
                        LocalDateTime dateTime = LocalDateTime.parse(key.get(0));
                        if (Period.between(nowDate, LocalDate.from(dateTime)).getDays() >= 0 && Period.between(nowDate, LocalDate.from(dateTime)). getDays() <= 7) {
                            String str = "|#|" + key.get(0) + "  |" + key.get(1) + "  |" + values.getFirst() + "  |" + Cinema.getTicketPrice(key.get(0)) + "|#|";
                            JLabel film = new JLabel(str);
                            c.add(film);
                        }
                    }
                }else if(box.getSelectedItem().equals("近三天")){
                    for (Map.Entry<List<String>, List<String>> entry : Cinema.getCinemaMap().entrySet()) {
                        List<String> key = entry.getKey();
                        List<String> values = Cinema.getCinemaMap().get(key);
                        LocalDateTime dateTime = LocalDateTime.parse(key.get(0));
                        if (Period.between(nowDate, LocalDate.from(dateTime)).getDays() >= 0 && Period.between(nowDate, LocalDate.from(dateTime)). getDays() <= 3) {
                            String str = "|#|" + key.get(0) + "  |" + key.get(1) + "  |" + values.getFirst() + "  |" + Cinema.getTicketPrice(key.get(0)) + "|#|";
                            JLabel film = new JLabel(str);
                            c.add(film);
                        }
                    }
                }else {
                    for (Map.Entry<List<String>, List<String>> entry : Cinema.getCinemaMap().entrySet()) {
                        List<String> key = entry.getKey();
                        List<String> values = Cinema.getCinemaMap().get(key);
                        LocalDateTime dateTime = LocalDateTime.parse(key.get(0));
                        if (Period.between(nowDate, LocalDate.from(dateTime)).getDays() >= 0 && Period.between(nowDate, LocalDate.from(dateTime)). getDays() <= 1) {
                            String str = "|#|" + key.get(0) + "  |" + key.get(1) + "  |" + values.getFirst() + "  |" + Cinema.getTicketPrice(key.get(0)) + "|#|";
                            JLabel film = new JLabel(str);
                            c.add(film);
                        }
                    }
                }

                ArrangementList.setVisible(true);
            }
        });

        filmSessionsList.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FilmSessionsList filmSessionsList = new FilmSessionsList(FrontDesk.this);
                filmSessionsList.setVisible(true);

            }
        });

        ticket.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TicketWindows ticketWindows = new TicketWindows(FrontDesk.this);
                ticketWindows.setVisible(true);


            }
        });

        returnTicket.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                LocalDateTime nowDateTime = LocalDateTime.now();

                JDialog refund = new JDialog(frame,"退票",true);
                refund.setBounds(720,405,410,400);
                Container c = refund.getContentPane();
                c.setLayout(new FlowLayout());

                JLabel label = new JLabel("请输入票ID：");
                JTextField ticketID = new JTextField(10);
                JButton choose = new JButton("选择");

                c.add(label);
                c.add(ticketID);
                c.add(choose);

                choose.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e){
                        boolean flag = false ;

                            for (Map.Entry<List<String>, String> entry : Cinema.getCinemaID().entrySet()) {
                                List<String> key = entry.getKey();
                                String values = Cinema.getCinemaID().get(key);
                                Duration duration = Duration.between(nowDateTime, LocalDateTime.parse(key.get(0)));
                                long hours = duration.toHours();
                                if(hours <= 1){
                                    if (ticketID.getText().equals(values)) {
                                        flag = true;
                                        Cinema.chooseCinemaSeat(key, false);
                                        Cinema.setCinemaRefund(key.get(0), Cinema.getTicketPrice(key.get(0)));
                                        JOptionPane.showMessageDialog(null,"成功");
                                    }
                                }
                            }

                            if(!flag) {
                            JOptionPane.showMessageDialog(null,"票不存在或者不在退票时间");
                        }

                    }
                });

                refund.setVisible(true);

            }
        });

        exitLogIN.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User.nowUser = null;
                JOptionPane.showMessageDialog(null,"已经退出登录");
                dispose();
            }
        });
    }

    private static class TicketWindows extends JDialog {
        private TicketWindows(FrontDesk frame) {
            super(frame, "前台操作界面", true);
            setBounds(720, 405, 400, 600);
            setLayout(new FlowLayout());

            HashMap<List<String>, List<String>> ticketMap = new HashMap<>();
            final int[] filmTicketSales = {0};
            final float[] filmTicketPrice = {0};


            JLabel label = new JLabel("请输入电影名称：");
            JTextField filmName = new JTextField(10);
            JLabel label1 = new JLabel("请输入观影时间：");
            JTextField filmTime = new JTextField(10);
            JLabel label2 = new JLabel("请输入观影影厅：");
            JTextField filmRoom = new JTextField(10);
            JLabel label3 = new JLabel("请输入座位号(排，列)：");
            JTextField rowRoom = new JTextField(3);
            JTextField colRoom = new JTextField(3);

            JButton choose = new JButton("选择");
            JButton buy = new JButton("结算");

            add(label);
            add(filmName);
            add(label1);
            add(filmTime);
            add(label2);
            add(filmRoom);
            add(label3);
            add(rowRoom);
            add(colRoom);
            add(choose);
            add(buy);

            buy.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    JDialog buyInfo = new JDialog(frame,"票信息",true);
                    buyInfo.setBounds(720,405,960,500);
                    Container c = buyInfo.getContentPane();
                    c.setLayout(new FlowLayout());

                    JLabel label0 = new JLabel ("票价：" + filmTicketPrice[0]+ "\n");
                    Cinema.setCinemaSales(filmTime.getText(),filmTicketSales[0]);

                    c.add(label0);
                    for(Map.Entry<List<String>, List<String>> entry : ticketMap.entrySet()) {
                        List<String> key = entry.getKey();
                        List<String> values = ticketMap.get(key);
                        Cinema.chooseCinemaSeat(key.get(0), key.get(1),true,key.get(2), key.get(3));
                        Cinema.setCinemaID(key.get(0), key.get(1),key.get(2), key.get(3),values.get(1));
                        JLabel label1 = new JLabel("电影名称：" + values.get(0) + "\n" + "观影影厅：" + key.get(1) + "\n" + "座位号：" + key.get(2) + "排" + key.get(3) + "列" + "\n" + "票号" + values.get(1) + "\n");
                        c.add(label1);
                    }

                    buyInfo.setVisible(true);
                }
            });

            choose.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    UUID uuid = UUID.randomUUID();
                    List<String> key1 = new ArrayList<>();
                    List<String> key2 = new ArrayList<>();

                    key1.add(filmTime.getText());
                    key1.add(filmRoom.getText());
                    key1.add(rowRoom.getText());
                    key1.add(colRoom.getText());

                    key2.add(filmName.getText());
                    key2.add(String.valueOf(uuid));

                    ticketMap.put(key1, key2);

                    filmTicketSales[0] ++;
                    filmTicketPrice[0] += Cinema.getTicketPrice(filmTime.getText());
                    JOptionPane.showMessageDialog(null,"已经添加");
                }
            });


        }
    }

    private static class FilmSessionsList extends JDialog {
        private FilmSessionsList(FrontDesk frame) {
            super(frame, "前台操作界面", true);
            setBounds(720, 405, 400, 600);
            setLayout(new FlowLayout());

            JLabel label = new JLabel("请输入播放时段：");
            JTextField filmSession = new JTextField(10);
            JLabel label1 = new JLabel("请输入播放影厅：");
            JTextField filmHall = new JTextField(10);
            JButton check = new JButton("查询");


            add(label);
            add(filmSession);
            add(label1);
            add(filmHall);
            add(check);

            check.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String room = filmHall.getText();
                    String time = filmSession.getText();
                    int totalSeat = 0;
                    int unsoldSeat = 0;


                    for(Map.Entry<List<String>, List<String>> entry : Cinema.getCinemaMap().entrySet()){
                    List<String> key = entry.getKey();
                    List<String> values = Cinema.getCinemaMap().get(key);
                    if(filmHall.getText().equals(key.get(1)) && filmSession.getText().equals(key.get(0))) {


                        JDialog seatList = new JDialog(frame, "座位图", true);
                        int p = 15 * Cinema.getRow(key.get(1));
                        int q = 30 * Cinema.getCol(key.get(1));
                        System.out.println(p+"\n"+q+"\n"+Cinema.getRow(key.get(1)));
                        seatList.setBounds(720, 405, p , q);
                        Container c = seatList.getContentPane();
                        seatList.setLayout(new FlowLayout());

                        JLabel row0 = new JLabel("///");
                        c.add(row0);

                        for (int i = 1; i <= Cinema.getCol(key.get(1)); i++) {
                            JLabel row = new JLabel(String.valueOf(i));
                            c.add(row);
                        }

                        for (int i = 0; i < Cinema.getRow(key.get(1)); i++) {
                            JLabel col = new JLabel(String.valueOf(i+1));
                            c.add(col);
                            for (int j = 0; j < Cinema.getCol(key.get(1)); j++) {
                                if (Cinema.ifHaveCinemaSeat(filmSession.getText(),filmHall.getText(), i, j)) {
                                    JLabel noSeat = new JLabel("x");
                                    c.add(noSeat);
                                    totalSeat++;
                                } else {
                                    JLabel haveSeat = new JLabel("o");
                                    c.add(haveSeat);
                                    unsoldSeat++;
                                    totalSeat++;
                                }
                            }
                        }
                        JLabel seatInfo = new JLabel("总座位数：" + totalSeat );
                        JLabel seatInfo1 = new JLabel("剩余座位数：" + unsoldSeat);
                        c.add(seatInfo);
                        c.add(seatInfo1);

                        seatList.setVisible(true);
                    }
                }
                }
            });
        }
    }
}
